using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using PersistenceObject.Framework;
using PersistenceObject.Framework.Expressions;

using FundSms.Server.Model;

namespace FundSms.Server.Monitor
{
    public class MTHelper
    {
        private static readonly string DataKey = "Sms";

        public static IList<EnterpriseMT> GetEnterprise()
        {
            IList<EnterpriseMT> listmt = new List<EnterpriseMT>();
            EnterpriseMT mt = null;

            IList<Enterprise> list = QueryOperate<Enterprise>.Create(DataKey).OrderBy(Enterprise._EnterpriseID).List();

            foreach (Enterprise e in list)
            {
                mt = new EnterpriseMT();
                mt.EnterpriseAccount = e.Account;
                mt.EnterpriseCode = e.EnterpriseCode;
                mt.EnterpriseName = e.EnterpriseName;

                using (DataSession ds = new DataSession(DataKey))
                {
                    ds.Open();

                    IDbConnection conn = ds.Connection;

                    System.Data.SqlClient.SqlCommand command = conn.CreateCommand() as System.Data.SqlClient.SqlCommand;
                    command.CommandText = "SELECT COUNT(1) FROM SMS_MT_" + mt.EnterpriseAccount;
                    command.CommandType = CommandType.Text;

                    try
                    {
                        int result = Convert.ToInt32(ds.ExecuteScalar(command));

                        mt.MTCount = result;
                    }
                    catch
                    {
                        mt.MTCount = 0;
                    }

                    ds.Close();
                }

                listmt.Add(mt);

            }

            return listmt;
        }

    }

    public class EnterpriseMT
    {
        private string enterprisecode;

        public string EnterpriseCode
        {
            get { return enterprisecode; }
            set { enterprisecode = value; }
        }
        private string enterpriseaccount;

        public string EnterpriseAccount
        {
            get { return enterpriseaccount; }
            set { enterpriseaccount = value; }
        }
        private string enterprisename;

        public string EnterpriseName
        {
            get { return enterprisename; }
            set { enterprisename = value; }
        }
        private int mtcount;

        public int MTCount
        {
            get { return mtcount; }
            set { mtcount = value; }
        }
    }
}
